<template>
    <div class="app-container">
      <el-row :gutter="20">
        <el-col :span="24" :xs="24">
          <el-form
            v-show="showSearch"
            ref="queryForm"
            :model="queryParams"
            :inline="true"
            label-width="68px"
          >
          <el-form-item label="订单编号" prop="orderNumber">
              <el-input
                v-model="queryParams.orderNumber"
                placeholder="请输入联系方式"
                clearable
                size="small"
                style="width: 180px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          <el-form-item label="下单人" prop="consignerName">
              <el-input
                v-model="queryParams.consignerName"
                placeholder="请输入取件码"
                clearable
                size="small"
                style="width: 180px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="手机号码" prop="phone">
              <el-input
                v-model="queryParams.phone"
                placeholder="请输入手机号"
                clearable
                size="small"
                style="width: 180px"
                @keyup.enter.native="handleQuery"
              />
              <el-button
                  style="margin-left: 50px;"
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="getExpressageOrder"
                >搜索</el-button>
            </el-form-item>
          </el-form>
  
          <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-save"
                size="mini"
                @click="open = true"
                >添加</el-button>
            </el-col>
          </el-row> -->
  
          <el-table :data="expressageOrderList" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column prop="orderNumber" label="订单编号" align="center" />
            <el-table-column prop="consignerName" label="下单人" align="center" />
            <el-table-column prop="phone" label="联系方式" align="center" />
            <el-table-column prop="address" label="地址" align="center" />
            <el-table-column prop="createTime" label="下单时间" align="center" />
            <!-- <el-table-column prop="state" label="状态" align="center">
                <template #default="scope">
                    <strong :class="[scope.row.state == 0 ? 'select1' : 'select2']">{{ scope.row.state == 0 ? "已送达" : "未送达" }}</strong>
                </template>
            </el-table-column> -->
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-save"
                    @click="handleUpdate(scope.row)"
                >生成快递</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-pagination
        :page-size.sync="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :current-page.sync="queryParams.pageNum"
        @current-change="getExpressageOrder"
        @size-change="getExpressageOrder"
      />
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body @close="onClose">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="14">
              <el-form-item label="订单编号" prop="orderNumber">
                <el-input
                  v-model="form.orderNumber"
                  placeholder="请生成订单编号"
                  maxlength="5"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="10">
                
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="14">
              <el-form-item label="取件码" prop="fetchGoodsNumber">
                <el-input
                  v-model="form.fetchGoodsNumber"
                  placeholder="请生成取件码"
                  maxlength="5"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="10">
                <el-button type="success" style="margin-left: 30px; margin-top: 4px;" size="small" @click="anewFetchGoodsNumber">点击生成取件码</el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="取件人" prop="phone">
                <el-input
                  v-model="form.fetchGoodsPerson"
                  placeholder="请输入取件人姓名"
                  maxlength="5"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="phone">
                <el-input
                  v-model="form.phone"
                  placeholder="请输入电话"
                  maxlength="11"
                  @input="handleInput"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="快递员" prop="courierName">
                <el-select
                    v-model="form.courierId"
                    placeholder="选择快递员"
                    clearable
                    style="width: 240px"
                >
                    <el-option v-for="item in staffList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="快递状态" prop="state">
                <el-select
                    v-model="form.state"
                    placeholder="选择状态"
                    clearable
                    style="width: 240px"
                    :disabled="true"
                >
                    <el-option :key="0" label="已送达" :value="0" />
                    <el-option :key="1" label="未送达" :value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="地址" prop="address">
                <el-input
                  v-model="form.address"
                  placeholder="请输入地址"
                  maxlength="30"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="" prop="courierName">
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预计时间" prop="predictTime">
                <el-date-picker
                    v-model="form.predictTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  // import { getToken } from '@/utils/auth'
  
  import { listExpressageOrder,addOrderCreate,delExpressageOrder } from '@/api/staff/order.js'
  import {
    listStaff
  }
  from '@/api/staff/staff.js'
  import { 
    queryStudyAll 
  } from "@/api/content/tag";
import { color } from 'echarts';
  export default {
    name: 'Parcel',
    data() {
      return {
        exportLoading:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNumber: undefined,
          consignerName: undefined,
          phone: undefined,
          createTime: undefined
        },
        title: '',
        // 是否显示弹出层
        open: false,
        // 表单校验
        rules: {
          // userName: [
          //   { required: true, message: '用户名称不能为空', trigger: 'blur' },
          //   {
          //     min: 2,
          //     max: 20,
          //     message: '用户名称长度必须介于 2 和 20 之间',
          //     trigger: 'blur'
          //   }
          // ],
          headNumber: [
            { required: true, message: '学号只能为8位数', trigger: 'blur',max:8,min:8 }
          ],
          password: [
            { min: 1,
              max: 1,
              required: true, 
              message: '用户密码必须有英文加数字组成', 
              trigger: 'blur' 
            }
          ],
        },
        // 角色选项
        roleOptions: [],
        fileList:[],
        // 显示搜索条件
        showSearch: true,
        staffList:[],
        // 遮罩层
        loading: true,
        // 用户表格数据
        floristList: null,
        expressageOrderList:[],
        // 总条数
        total: 0,
        // 选中数组
        ids: [],
        // 表单参数
        form:{
            delId:undefined,
            id:undefined,
            orderNumber:undefined,
            phone:undefined,
            fetchGoodsNumber:undefined,
            state:1,
            fetchGoodsPerson:undefined,
            courierId:undefined,
            courierName:undefined,
            address:undefined,
            predictTime: undefined
        }
      }
    },
    watch: {},
    created() {
      this.getExpressageOrder()
      this.getStaff()
    },
    methods: {
     onClose() {
        this.reset()
     },
     getStaff() {
        listStaff(this.queryParams).then((response) => {
            this.staffList = response.rows
        })
      },
     anewOrderNumber(){
        this.form.orderNumber = Math.floor(Math.random() * 900000000000) + 1
     },
    //  生成一个四位数的随机数字
     anewFetchGoodsNumber(){
        var number = Math.floor(Math.random() * 10000)
        while(number.toString().length < 4){
            number = Math.floor(Math.random() * 10000)
        }
        this.form.fetchGoodsNumber = String.fromCharCode(Math.floor(Math.random() * 26) + 65)+'-'+ (Math.floor(Math.random() * 9)+1) +'-'+number
     },
     handleInput(value) {
      const reg = /^[\d\.]+$/;
      if (value && !reg.test(value)) {
        this.form.age = value.replace(/[^\d\.]/g, '');
        this.form.phone = value.replace(/[^\d\.]/g, '');
      }
     },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getExpressageOrder()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      getExpressageOrder() {
        listExpressageOrder(this.queryParams).then((response) => {
            this.expressageOrderList = response.rows
            this.total = response.total
        })
      },
      // 表单重置
      reset() {
        this.form = {
          phone: undefined,
          id: undefined,
          orderNumber: undefined,
          fetchGoodsNumber: undefined,
          fetchGoodsPerson: undefined,
          courierId: undefined,
          courierName: undefined,
          address: undefined,
          state: 0,
          delId:undefined
        }
        this.resetForm('form')
      },
      /** 生成订单按钮操作 */
      handleUpdate(row) {
        this.open = true
        this.form.orderNumber = row.orderNumber
        this.form.delId = row.id
        this.form.fetchGoodsNumber = row.fetchGoodsNumber
        this.form.fetchGoodsPerson = row.consignerName
        this.form.phone = row.phone
        this.form.courierId = row.courierId
        this.form.courierName = row.courierName
        this.form.address = row.address
        this.form.state = row.state
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal
          .confirm('是否确认删除订单编号为"' + row.orderNumber + '"的数据项？')
          .then(function() {
            return delExpressageOrder(ids)
          })
          .then(() => {
            this.getExpressageOrder()
            this.$modal.msgSuccess('删除成功')
          })
          .catch(() => {})
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.form.id !== undefined) {
              updateExpressage(this.form).then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getExpressageOrder()
                this.reset()
              })
            } else {
              //添加走这段逻辑
              addOrderCreate(this.form).then((response) => {
                if(response.code == 200){
                    this.$modal.msgSuccess('创建订单成功')
                    this.open = false
                    delExpressageOrder(this.form.delId).then((response) => {
                        this.getExpressageOrder()
                    })
                    this.reset()
                } else {
                    this.$modal.msgSuccess('创建订单失败')
                }
                
              })
            }
          }
        })
      }
    }
  }
  </script>
  <style scoped>
  div .upload-demo {
    /* padding-left: 80px; */
  }
  .el-col .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
  
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .select2 {
    color: red;
    
  }
  .select1 {
    color: green;
  }
  </style>
  